package io.github.yotta.export.web.manager;

import io.github.yotta.common.api.model.ApiPageRequest;
import io.github.yotta.export.core.export.ExcelDataExporter;
import io.github.yotta.export.core.export.ExcelExporterExecutor;
import io.github.yotta.export.core.model.DataExportResult;
import io.github.yotta.export.core.model.XlsxDataExportResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;
import java.util.function.Consumer;

/**
 * @author yotta
 * @date 2023/10/30 17:04
 */
@Slf4j
@Component
@SuppressWarnings({"rawtypes", "unchecked", "unused"})
public class ExcelExportManagerImpl implements ExcelExportManager {

    private final ExcelExporterExecutor exporterExecutor;
    private final ExcelExporterRegister excelExporterRegister;

    public ExcelExportManagerImpl(ExcelExporterExecutor exporterExecutor, ExcelExporterRegister excelExporterRegister) {
        this.exporterExecutor = exporterExecutor;
        this.excelExporterRegister = excelExporterRegister;
    }

    @Override
    public DataExportResult exportExcel(String name, ApiPageRequest exportQueryParam) {
        final ExcelDataExporter exporter = excelExporterRegister.get(name);
        return exporterExecutor.exportXlsx(exportQueryParam, exporter);
    }

    @Override
    public Future<XlsxDataExportResult> exportExcelAsync(String name, ApiPageRequest exportQueryParam, Consumer<XlsxDataExportResult> onComplete) {
        final ExcelDataExporter exporter = excelExporterRegister.get(name);
        return exporterExecutor.exportXlsxAsync(exportQueryParam, exporter, onComplete);
    }
}
